package com.maxima.attendance.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.attendance.daos.TimeLogDAO;
import com.maxima.attendance.daos.UserDAO;
import com.maxima.attendance.pojos.Employee;
import com.maxima.attendance.pojos.TimeLog;
import com.opensymphony.xwork2.ActionSupport;

public class CheckInAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String password;
	private Map<String, Object> map;
	// for logging
	static Logger logger = Logger.getLogger(ProjectAction.class.getName());

	@Override
	public String execute() throws Exception {
		if (SessionManager.validateAdminSession()) {
			map.put(SessionManager.SELECTED_PROFILE, null);
			map.put(SessionManager.SELECTED_PROFILE, new UserDAO().getEmployee(this.getId()));
			return "admin";
		} else if (SessionManager.validateUserSession()) {
			map.put(SessionManager.SELECTED_PROFILE, null);
			map.put(SessionManager.SELECTED_PROFILE, new UserDAO().getEmployee(this.getId()));
			return "user";
		} else {
			return ERROR;
		}
	}

	public String checkIn() {
		if (SessionManager.validateAdminSession()) {
			return checkLogin() ? "admin" : ERROR;
		} else if (SessionManager.validateUserSession()) {
			return checkLogin() ? "user" : ERROR;
		} else {
			return ERROR;
		}
	}

	private boolean checkLogin() {
		this.password = ServletActionContext.getRequest().getParameter("password");
		Employee emp = (Employee) map.get(SessionManager.SELECTED_PROFILE);
		TimeLogDAO timeLogDAO = new TimeLogDAO();
		TimeLog timeLog = null;
		if (emp.getPassword().equals(password)) {
			Date date = new Date();
			SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
			if (emp.getLoggedIn() || emp.getLoggedOut()) {
				timeLog = timeLogDAO.addTimeOut(emp.getTimeLogId(), date);
				this.map.put("message", "Logged out at " + timeFormat.format(date));
				logger.info("Employee " + emp.getFirstName() + " Id - " + emp.getId() + " Logged Out Successfully");
				this.map.put("errorMessage", null);
			} else if (emp.getAbsent()) {
				timeLog = new TimeLog(new Date(), emp.getId());
				timeLog.setLoginTime(new Date());
				timeLog.setComment("none");
				timeLogDAO.addNewTimeLog(timeLog);
				this.map.put("message", "Logged in at " + timeFormat.format(date));
				logger.info("Employee " + emp.getFirstName() + " Id - " + emp.getId() + " Logged In Successfully");
				this.map.put("errorMessage", null);
			}
			emp.setTimeLog(timeLog);
			if (InitializeList.employeeList.contains(emp)) {
				int i = InitializeList.employeeList.indexOf(emp);
				InitializeList.employeeList.remove(i);
				InitializeList.employeeList.add(i, emp);
			}
			map.put(SessionManager.CURRENT_EMPLOYEE_LIST, InitializeList.employeeList);
			this.setSession(map);
			return true;
		} else {
			this.map.put("message", "Please enter correct password!!");
			// this.map.put("message", null);
			this.setSession(map);
			return false;
		}
	}

	public String changeUserPassword() {
		String oldPassword;
		String newPassword;
		String confirmPassword;
		Employee emp = (Employee) map.get(SessionManager.SELECTED_PROFILE);
		oldPassword = ServletActionContext.getRequest().getParameter("old_password");
		newPassword = ServletActionContext.getRequest().getParameter("new_password");
		confirmPassword = ServletActionContext.getRequest().getParameter("confirmPassword");
		if (emp.getPassword().equals(oldPassword)) {
			if (newPassword.equals(confirmPassword)) {
				new UserDAO().changePassword(emp.getId(), ServletActionContext.getRequest().getParameter("new_password"));
				this.map.put("message", "Your password has been changed successfully!!");
				logger.info("Employee " + emp.getFirstName() + " Id - " + emp.getId() + " Changed his password Successfully");
				this.map.put("errorMessage", null);
				return SUCCESS;
			} else {
				this.map.put("message", "Please enter New & confirm password same");
				// this.map.put("message", null);
				this.setSession(map);
				return ERROR;
			}
		} else {
			this.map.put("message", "Please enter correct old password!!");
			// this.map.put("message", null);
			this.setSession(map);
			return ERROR;
		}
	}

	public String checkLoginType() {
		if (SessionManager.validateAdminSession())
			return "admin";
		else if (SessionManager.validateUserSession())
			return "user";
		return ERROR;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		this.map = map;
	}
}
